
#include "stdafx.h"
#include <windows.h>
#include <assert.h>
#include "BladeMP3EncDLL.h"

HINSTANCE hLameDLL;
BEINITSTREAM		pbeInitStream=NULL;
BEENCODECHUNK		pbeEncodeChunk=NULL;
BEDEINITSTREAM		pbeDeinitStream=NULL;
BECLOSESTREAM		pbeCloseStream=NULL;
BEVERSION			pbeVersion=NULL;
BEWRITEVBRHEADER	pbeWriteVBRHeader=NULL;

int initLame() {

	hLameDLL = LoadLibrary(L"lame_enc.dll");
	if ( !hLameDLL ) {
		fprintf(stderr,"Error loading lame_enError loading lame_enc.DLL");
		return ERR_LOAD_LAME_DLL;
	}

	pbeInitStream	= (BEINITSTREAM) GetProcAddress(hLameDLL, TEXT_BEINITSTREAM);
	pbeEncodeChunk	= (BEENCODECHUNK) GetProcAddress(hLameDLL, TEXT_BEENCODECHUNK);
	pbeDeinitStream	= (BEDEINITSTREAM) GetProcAddress(hLameDLL, TEXT_BEDEINITSTREAM);
	pbeCloseStream	= (BECLOSESTREAM) GetProcAddress(hLameDLL, TEXT_BECLOSESTREAM);
	pbeVersion		= (BEVERSION) GetProcAddress(hLameDLL, TEXT_BEVERSION);
	pbeWriteVBRHeader= (BEWRITEVBRHEADER) GetProcAddress(hLameDLL,TEXT_BEWRITEVBRHEADER);

	if(!pbeInitStream || !pbeEncodeChunk || !pbeDeinitStream || !pbeCloseStream || !pbeVersion || !pbeWriteVBRHeader)
	{
		fprintf(stderr, "Unable to get LAME interfaces");
		return ERR_LOAD_LAME_INTERFACE;
	}

	return 0;

}



BE_ERR beInitStream( PBE_CONFIG pbeConfig, PDWORD dwSamples, PDWORD dwBufferSize, PHBE_STREAM phbeStream )
{
	return (*pbeInitStream)(pbeConfig,dwSamples,dwBufferSize,phbeStream);
}

BE_ERR beEncodeChunk( HBE_STREAM hbeStream, DWORD nSamples, PSHORT pSamples, PBYTE pOutput, PDWORD pdwOutput )
{
	return  (*pbeEncodeChunk)(hbeStream, nSamples, pSamples, pOutput, pdwOutput );
}

BE_ERR beDeinitStream( HBE_STREAM hbeStream, PBYTE pOutput, PDWORD pdwOutput )
{
	return (*pbeDeinitStream)( hbeStream, pOutput, pdwOutput );
}

BE_ERR beCloseStream( HBE_STREAM hbeStream )
{
	return  (*pbeCloseStream)( hbeStream );
}


VOID beVersion( PBE_VERSION pbeVer )
{
	(*pbeVersion) ( pbeVer );
}

VOID beWriteVBRHeader( LPCSTR lpszFileName )
{
	(*pbeWriteVBRHeader)(lpszFileName);
}
